package com.lan.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

public class DateUtils {

    static FastDateFormat sdf_YYYY_MM_DD_HHMMSS = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    static FastDateFormat sdf_YYYY_M_D_HMS = FastDateFormat.getInstance("yyyy-M-d H:m:s", Locale.CHINA);

    static FastDateFormat sdf_YYYY_M_D_HM = FastDateFormat.getInstance("yyyy-M-d H:m", Locale.CHINA);

    static FastDateFormat sdf_YYYYMMDDHHmmss = FastDateFormat.getInstance("yyyyMMddHHmmss", Locale.CHINA);

    static FastDateFormat sdf_YYYYMMDDHms = FastDateFormat.getInstance("yyyyMMddHms", Locale.CHINA);

    static FastDateFormat sdf_YYYY_MM_DD = FastDateFormat.getInstance("yyyy-MM-dd", Locale.CHINA);

    static FastDateFormat sdf_YYYY_M_D = FastDateFormat.getInstance("yyyy-M-d", Locale.CHINA);


    static FastDateFormat sdf_YYYYMMDD = FastDateFormat.getInstance("yyyyMMdd", Locale.CHINA);


    static FastDateFormat sdf_YYYY__M__D = FastDateFormat.getInstance("yyyy/M/d", Locale.CHINA);


    static FastDateFormat sdf_YYYY__M__D_HMS = FastDateFormat.getInstance("yyyy/M/d H:m:s", Locale.CHINA);

    static FastDateFormat sdf_YYYY__M__D_HM = FastDateFormat.getInstance("yyyy/M/d H:m", Locale.CHINA);

    static FastDateFormat sdf_YYYYMD_HMS = FastDateFormat.getInstance("yyyyMd H:m:s", Locale.CHINA);

    static FastDateFormat sdf_YYYYMD_HM = FastDateFormat.getInstance("yyyyMd H:m", Locale.CHINA);


    public static Date str2Date_YYYY_MM_DD(String str) throws ParseException {
        return sdf_YYYY_MM_DD.parse(str);
    }

    public static Date str2Date_YYYY_MM_DD_HHMMSS(String str) throws ParseException {
        return sdf_YYYY_MM_DD_HHMMSS.parse(str);
    }

    public static Date str2Date_YYYYMMDDHHmmss(String str) throws ParseException {
        return sdf_YYYYMMDDHHmmss.parse(str);
    }

    public static Date str2Date_YYYYMMDDHms(String str) throws ParseException {
        return sdf_YYYYMMDDHms.parse(str);
    }


    public static Date str2Date_YYYYMMDD(String str) throws ParseException {
        return sdf_YYYYMMDD.parse(str);

    }

    public static Date str2Date_YYYY_M_D(String str) throws ParseException {
        return sdf_YYYY_M_D.parse(str);
    }


    public static Date str2Date_YYYY_M_D_HMS(String str) throws ParseException {
        return sdf_YYYY_M_D_HMS.parse(str);
    }

    public static Date str2Date_YYYY_M_D_HM(String str) throws ParseException {
        return sdf_YYYY_M_D_HM.parse(str);

    }

    public static Date str2Date_YYYY__M__D_HMS(String str) throws ParseException {
        return sdf_YYYY__M__D_HMS.parse(str);
    }


    public static Date str2Date_YYYY__M__D_HM(String str) throws ParseException {
        return sdf_YYYY__M__D_HM.parse(str);

    }


    public static Date str2Date_YYYYMD_HMS(String str) throws ParseException {
        return sdf_YYYYMD_HMS.parse(str);
    }

    public static Date str2Date_YYYY_HM(String str) throws ParseException {
        return sdf_YYYYMD_HM.parse(str);
    }

    public static Date str2Date_YYYY__M__D(String str) throws ParseException {
        return sdf_YYYY__M__D.parse(str);

    }


    public static String date2Str_YYYYMMDDHHmmss(Date date) {
        return sdf_YYYYMMDDHHmmss.format(date);

    }


    public static LocalDateTime dateToLocalDateTime(final Date date) {
        if (null == date) {
            return null;
        }
        final Instant instant = date.toInstant();
        final ZoneId zoneId = ZoneId.systemDefault();
        final LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }


    public static Date localDateTimeToDate(final LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return null;
        }
        final ZoneId zoneId = ZoneId.systemDefault();
        final ZonedDateTime zdt = localDateTime.atZone(zoneId);
        final Date date = Date.from(zdt.toInstant());
        return date;
    }


    public static int daysBetween(Date start, Date end) {
        start = getDayStart(start);
        end = getDayStart(end);
        long l = end.getTime() - start.getTime();
        if (l == 0) {
            return 0;
        }
        return (int) (l / (1000 * 3600 * 24));
    }

    /**
     * 起始时间
     *
     * @param date
     * @throws
     * @Title: getDayStart
     */
    public static Date getDayStart(Date date) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime t = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0);
        return localDateTimeToDate(t);
    }


    /**
     * t1 大于等于t 返回 true
     *
     * @param t
     * @param t1
     * @return
     */
    public static boolean gt(Date t, Date t1) {
        if (DateUtils.daysBetween(t, t1) >= 0) {
            return true;
        }
        return false;
    }


    public static Date get(String str) throws ParseException {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        if (str.length() == 10) {
            return str2Date_YYYY_MM_DD(str);
        } else if (str.length() == 19) {
            return str2Date_YYYY_MM_DD_HHMMSS(str);
        }
        return null;
    }


    public static void main(String[] args) throws ParseException {
        Date curDate = new Date();
//        System.out.println(date2Str_YYYY_MM_DD_HHMMSS(getDayStart(curDate)));
//        System.out.println(date2Str_YYYY_MM_DD_HHMMSS(getDayEnd(curDate)));
//
//        System.out.println(date2Str_YYYY_MM_DD_HHMMSS(getDayStart(getFirstDayOfMonth(curDate))));
//        System.out.println(date2Str_YYYY_MM_DD_HHMMSS(getDayEnd(getLastDayOfMonth(curDate))));

//        System.out.println(verifyTrack("2019-12-31 09:00:00", "2019-12-31 09:00:00"));

//        Calendar calendar = Calendar.getInstance();
//        calendar.set();
//        FastDateFormat fdf = FastDateFormat.getInstance("yyyy-M-d H:m:s", ,Locale.CHINA);
//        System.out.println(fdf.format(new Date()));
        String dateStr = "2022-07-29 10:10:21";
//        String dateStr = "2022-07-29";
//        Date now = fdf.parse(dateStr);
//        System.out.println(now);

//        LocalDateTime localDateTime = LocalDateTime.from()

//        LocalDateTime local = LocalDateTime.from()
//        LocalDateTime.from(sdf_YYYY_MM_DD_HHMMSS.parse())

//        LocalDateTime localDateTime = LocalDateTime.from(str2Date_YYYY_MM_DD_HHMMSS(dateStr));
//        System.out.println(date2Str_YYYYMMDDHHmmss(getDayStart(new Date())));

//        verifyTrack("2022-02-01 10:10:10", "2022-02-25 10:10:10");


//        System.out.println(date2Str_YYYYMMDDHHmmss(localDateTimeToDate(LocalDateTime.now().minusMonths(6))));
    }

//    static void print(String str, boolean bool) throws ParseException {
//        Date date = str2Date(str, bool);
//        String t = date2Str_YYYY_MM_DD_HHMMSS(date);
//        System.out.println(str + "     " + bool + "       " + t);
//
//    }


}
